<?php
/**
 * communit.as
 * @copyright (C)2008 Jaybill McCarthy, All Rights Reserved.
 * @category communitas
 * @package communitas
 * @author Jaybill McCarthy
 * @link http://communit.as communit.as
 * @license http://communit.as/docs/license License
 */

/**
 *
 * @package communitas
 * @subpackage forum_controllers
 * @license http://communit.as/docs/license License 
 */

class Forum_ForumController extends Forum_Controller_Action_Abstract {
	var $_last_login_on = null;

	function init(){	
		parent::init();				
	}

	function indexAction(){
		// all topics in this forum
		$request = new Cts_Request($this->getRequest());
		$forums_table = new Forums();
		$posts_table = new Posts();
		$views_index_table = new TopicviewsIndex();
		$views_table = new Topicviews();
		$where = $forums_table->getAdapter()->quoteInto('id = ?',$request->id);
		$forum = $forums_table->fetchRow($where);
		if(is_null($forum)){
			$this->_redirect("/forum");
		} else {
			$this->view->forum = $forum->toArray();
		}

		$topics_table = new TopicsIndex();
		$topics_where = $forums_table->getAdapter()->quoteInto('forum_id = ?',$request->id);
		$per_page = Cts_Registry::get('forum_topics_per_page','forum');		
		if($request->has('page')){
			$page = $request->page;
		} else {
			$page = 0;
		}
		$total = $topics_table->getCountByWhereClause($topics_where);		
		$url = "/forum/forum/index/id/".$forum->id;
		$params = array();
		$this->makePager($page,$per_page,$total,$url,$params);

		$topics = $topics_table->fetchAll($topics_where,null,$per_page,$per_page * $page);
		if(count($topics) > 0){
			
			$tmp_topics = array();
			foreach($topics as $topic){
				$tmp_topic = $topic->toArray();
				$tmp_topic['replies'] = ($posts_table->getCountByWhereClause('topic_id = '.$topic->id)) - 1;
				$tmp_topic['views'] = (int)$views_index_table->getCount($topic->id);

				$last_post = $posts_table->getLatestByTopicId($topic->id, null);

				if(!is_null($last_post)){
					$tmp_topic['last_post'] = $last_post->toArray();
					$last_viewed = $views_table->getLastTimeViewed($tmp_topic['id']);					
					$last_post_created =  strtotime($tmp_topic['last_post']['created_on']);					

					if($last_viewed == null or $last_post_created > $last_viewed){					
						$tmp_topic['new'] = true;				
					}				
				}
				$tmp_topics[] = $tmp_topic;
			}		
			$this->view->topics = $tmp_topics;
		}
	}
	
	function deleteAction(){
		$request = new Cts_Request($this->getRequest());		
		$forums_table = new Forums();
		$topics_table = new Topics();
		$where = $forums_table->getAdapter()->quoteInto("id = ?",$request->id);
		$forum = $forums_table->fetchRow($where);
		if(is_null($forum)){
			$this->_redirect("/default/auth/missing");
		}		
		$this->view->id = $forum->id;
		$this->view->pagetitle = 'Delete "'.$forum->title.'"';		
 
		$alt_forum_where = $forums_table->getAdapter()->quoteInto("id <> ?",$request->id);
		$all_forums = $forums_table->fetchAll($alt_forum_where,'title');
		$tmp_forums = array();
		$tmp_forums[null] = "Nowhere. Delete All Topics";
		foreach($all_forums as $forum_item){
			$tmp_forums[$forum_item->id] = $forum_item->title;
		}
		$this->view->forums = $tmp_forums;
		
		if($this->_request->isPost()){
			
			
			if(!is_null($request->move_to_forumid)){
				$topic_where = $topics_table->getAdapter()->quoteInto("forum_id = ?",$forum->id);				
				$data = array('forum_id' => $request->move_to_forumid);							
				$topics_table->update($data,$topic_where);				
			} 
			$forums_table->delete($where);
			$this->_redirect('/forum');
		}
		
		
	}

	function editAction(){
		$request = new Cts_Request($this->getRequest());
		$forums_table = new Forums();
		$subjects_table = new Subjects();
		$this->view->subjects = $subjects_table->getSubjectsArray();		
		if(!$request->has('id')){					
			$this->view->pagetitle = "New Forum";
		} else {
			$this->view->pagetitle = "Edit Forum";
			$where = $forums_table->getAdapter()->quoteInto('id = ?',$request->id);
			$forum = $forums_table->fetchRow($where);
			if(!is_null($forum)){
				$this->view->id = $forum->id;
				$this->view->title = $forum->title;
				$this->view->description = $forum->description;
				$this->view->active = $forum->active;
				$this->view->mode = "edit"; 
			}
		}

		if ($this->_request->isPost()) {
			$errors = array();

			if (!Cts_Validate::checkLength($request->id, 1, 16)) {
				$errors[] = $this->_T("Forum ID must be between 1 and 16 chars");
			}

			if (!Cts_Validate::checkLength($request->title, 1, 256)) {
				$errors[] = $this->_T("Forum Title must be between 1 and 256 chars");
			}

			if ($forums_table->getCountByWhereClause($where) > 0 and $request->mode != "edit") {
				$errors[] = $this->_T("The forum ID %s is already in use", $request->id);
			}
			
			if(count($errors) > 0){
				$this->view->errors = $errors;
				$this->view->id = $request->id;
				$this->view->title = $request->title;
				$this->view->description = $request->description;
				$this->view->active = $request->active;
			} else {
				$data = array(
					'title' => $request->title,
					'description' => $request->description,
					'active' => (int)$request->active,
					'subject_id' => $request->subjectid,					
				);
				
				
				if($request->mode == "edit"){
					$forums_table->update($data,$where);					
				} else {						
					$data['id'] = $request->id;
					$forums_table->insert($data);
				}
				
				$this->_redirect('/forum');
			}
			
		}
		
		
	}

}